﻿#include "FlowChartWidget.h"
#include "FlowView.h"
#include <QWheelEvent>
#include <QScrollBar>
#include <QHBoxLayout>
#include <QTimer>


// 构造函数
FlowChartWidget::FlowChartWidget(QWidget* parent) : QWidget(parent)
{
    // 初始化滚动区域
    m_scrollArea = new QScrollArea(this);
    m_scrollArea->setFrameShape(QFrame::NoFrame);
    m_scrollArea->setWidgetResizable(false);
    m_scrollArea->setAlignment(Qt::AlignCenter);
    m_scrollArea->setStyleSheet("QScrollArea { background: transparent; }");

    // 初始化绘图视图并关联数据模型
    m_flowView = new FlowView();
    m_scrollArea->setWidget(m_flowView);
    m_flowView->setScrollArea(m_scrollArea);
    m_scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    centerView();
    // 设置布局
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(m_scrollArea);
    setLayout(layout);
}

 //在源文件（如 flowchartwidget.cpp）中实现
void FlowChartWidget::resizeEvent(QResizeEvent* event) {
    QWidget::resizeEvent(event);  // 先调用基类处理

    centerView();
}

void FlowChartWidget::centerView()
{
    if (m_scrollArea && m_flowView) {
        const QSize drawingSize = m_flowView->size();
        const QSize viewportSize = m_scrollArea->viewport()->size();
        m_scrollArea->horizontalScrollBar()->setValue((drawingSize.width() - viewportSize.width()) / 2);
        m_scrollArea->verticalScrollBar()->setValue((drawingSize.height() - viewportSize.height()) / 2);
    }
}
